package org.infodavid.common.services;

import java.util.List;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.infodavid.common.dto.IOrganizationListItem;
import org.infodavid.common.impl.services.validators.DefaultOrganizationValidator;
import org.infodavid.common.model.IOrganization;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.model.criteria.IOrganizationCriteria;
import org.infodavid.common.persistence.IQueryParameter;
import org.infodavid.common.reflect.InstantiationHelper;
import org.infodavid.common.services.exceptions.ServiceException;

/**
 * The Class OrganizationService.
 */
public abstract class OrganizationService extends
    AbstractEntityService<IOrganizationListItem,IOrganization,IOrganizationCriteria> {

	/** The singleton. */
	private static final OrganizationService SINGLETON;

	/** The Constant log. */
	private static final Log LOGGER = LogFactory.getLog(OrganizationService.class);

	static {
		try {
			SINGLETON = InstantiationHelper.getInstance().newInstance(OrganizationService.class);
		}
		catch (final Exception e) {
			LOGGER.fatal("Can not instantiate the " + OrganizationService.class.getName(), e);

			throw new Error(e);
		}
	}

	/**
	 * returns the singleton.
	 * @return <code>CompanyService</code>
	 */
	public static OrganizationService getInstance() {
		return SINGLETON;
	}

	/**
	 * Instantiates a new service.
	 */
	protected OrganizationService() {
		super(IOrganization.class, new DefaultOrganizationValidator(), ModelFactory.getInstance()
		    .getBuilder(IOrganization.class));
	}

	/**
	 * Find by name.
	 * @param context the context
	 * @param value the value
	 * @param entities the entities
	 * @param parameters the parameters
	 * @return the long
	 * @throws PersistenceException the persistence exception
	 * @throws ServiceException the service exception
	 */
	public abstract long findByName(IApplicationContext context, String value,
	    List<IOrganizationListItem> entities, List<IQueryParameter> parameters)
	    throws PersistenceException, ServiceException;
}
